<?php
	class tivi{
		public function getTiviByWhere($where, $order){
			
			$db = new db();
			$db->getInstance();
			$sql = "select t.* , h.TenHang, l.TenLoai
					from tbltivi t INNER JOIN tblhangsanxuat h on t.idHangSanXuat = h.id
								INNER JOIN tblloaitv l on t.idLoaiTv = l.id";
			if($where != '') $sql .= ' where ' . $where; 
			if($order != '') $sql .= ' order by ' . $order; 
			$rs = mysql_query($sql); //echo $sql; die;
			if(mysql_num_rows($rs) > 0){
				while($rc = mysql_fetch_object($rs)){
					$lstTivi[] = $rc;	
				}
				return $lstTivi;
			}
			else{
				return null;	
			}
		}
		
		public function getTiviById($tivi_id){
			
			$db = new db();
			$db->getInstance();
			$sql = "select t.* , h.TenHang, l.TenLoai
					from tbltivi t INNER JOIN tblhangsanxuat h on t.idHangSanXuat = h.id
								INNER JOIN tblloaitv l on t.idLoaiTv = l.id where t.id = " . $tivi_id;
			
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs) > 0){
				
				$Tivi = mysql_fetch_object($rs);	
				
				return $Tivi;
			}
			else{
				return null;	
			}
		}
		
		public function getTiviByWherePaging($where, $order, $pos){
			
			$db = new db();
			$db->getInstance();
			$sql = "select t.* , h.TenHang, l.TenLoai
					from tbltivi t INNER JOIN tblhangsanxuat h on t.idHangSanXuat = h.id
								INNER JOIN tblloaitv l on t.idLoaiTv = l.id";
			if($where != '') $sql .= ' where ' . $where; 
			if($order != '') $sql .= ' order by ' . $order;
			$sql .= ' limit '.$pos.','.__PAGE_SIZE; //echo $sql;die;
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs) > 0){
				while($rc = mysql_fetch_object($rs)){
					$lstTivi[] = $rc;	
				}
				return $lstTivi;
			}
			else
				return null;
		}
		
		public function getKichThuocTivi(){
			
			$db = new db();
			$db->getInstance();
			$sql = "select KichThuoc from tblTivi group by KichThuoc";
			
			$rs = mysql_query($sql);
			while($rc = mysql_fetch_object($rs)){
				$lstTivi[] = $rc;	
			}
			return $lstTivi;
		}
		
		public function searchTivi($where,$pos){
			
			
			$sql = "select t.* , h.TenHang, l.TenLoai
					from tbltivi t INNER JOIN tblhangsanxuat h on t.idHangSanXuat = h.id
								INNER JOIN tblloaitv l on t.idLoaiTv = l.id 
					where ".$where . " limit ".$pos. ",".__PAGE_SIZE;
					//echo $sql; die;
			$db = new db();
			$db->getInstance();
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs) > 0){
				while($rc = mysql_fetch_object($rs)){
					$lstTivi[] = $rc;	
				}
				//echo '<pre>'; var_dump($lstTivi);die;
				return $lstTivi;
			}
			else return null;
		}
		
		public function insert($name,$phangiai,$kichthuoc,$giathat,$giaban,$soluong,$images,$images2,$images3,$images4,$images5,$tinhnang,$mota,$thongsokythuat,$hangsx,$loaitv){
			$db = new db();
			$db->getInstance();
			$sql = "insert tbltivi(TenTivi,PhanGiai,KichThuoc,GiaThat,GiaBan,SoLuong,Images,Images2,Images3,Images4,Images5,TinhNang,MoTa,ThongSoKyThuat,idHangSanXuat,idLoaiTv,TrangThai) values(N'".$name."',N'".$phangiai."',".$kichthuoc.",'".$giathat."','".$giaban."',".$soluong.",'".$images."','".$images2."','".$images3."','".$images4."','".$images5."',N'".$tinhnang."',N'".$mota."',N'".$thongsokythuat."',".$hangsx.",".$loaitv.",1)";
			//echo $sql; die;
			mysql_query("SET NAMES 'utf8'"); 
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function update($name,$phangiai,$kichthuoc,$giathat,$giaban,$soluong,$images,$images2,$images3,$images4,$images5,$tinhnang,$mota,$thongsokythuat,$hangsx,$loaitv,$trangthai,$id){
			$db = new db();
			$db->getInstance();
			$sql = "update tbltivi set TenTivi='$name',PhanGiai='$phangiai',KichThuoc=$kichthuoc,GiaThat='$giathat',GiaBan='$giaban',SoLuong=$soluong,Images='$images',Images2='$images2',Images3='$images3',Images4='$images4',Images5='$images5',TinhNang='$tinhnang',MoTa='$mota',ThongSoKyThuat='$thongsokythuat',idHangSanXuat=$hangsx,idLoaiTv=$loaitv,TrangThai=$trangthai where id= $id";
			//echo $sql; die;
			
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function delete($id){
			$db = new db();
			$db->getInstance();
			$sql = 'delete from tbltivi where id='.$id;
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function updateSoLuongById($soluong,$id){
			$db = new db();
			$db->getInstance();
			$sql = "update tbltivi set SoLuong=$soluong where id= $id";
			//echo $sql; die;
			
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function updateTrangThai($trangthai,$tivi_id){
			$db = new db();
			$db->getInstance();
			$sql = "update tbltivi set TrangThai=$trangthai where id= $tivi_id";
			//echo $sql; die;
			
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function updateTrangThaiByHang($trangthai,$hang_id){
			$db = new db();
			$db->getInstance();
			$sql = "update tbltivi set TrangThai=$trangthai where idHangSanXuat= $hang_id";
			//echo $sql; die;
			
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
		
		public function updateTrangThaiByLoai($trangthai,$loai_id){
			$db = new db();
			$db->getInstance();
			$sql = "update tbltivi set TrangThai=$trangthai where idLoaiTv= $loai_id";
			//echo $sql; die;
			
			$rs = mysql_query($sql);
			if($rs) return true;
			else return false;	
		}
	}
?>